<template>
  <div class="basic-info">
    <div class="info-item">
      <span class="label">账号：</span>
      <span class="value account_value">{{ userInfo.account }}</span>
      <el-link type="primary" @click="showPasswordModal = true">修改密码</el-link>
    </div>
    <div class="info-item">
      <span class="label">姓名：</span>
      <span class="value">{{ userInfo.name }}</span>
    </div>
    <div class="info-item">
      <span class="label">部门：</span>
      <span class="value">{{ userInfo.dept }}</span>
    </div>
    <div class="info-item">
      <span class="label">岗位：</span>
      <span class="value">{{ userInfo.post }}</span>
    </div>
    <div class="info-item">
      <span class="label">手机号：</span>
      <span class="value">{{ userInfo.phone }}</span>
    </div>
    <div class="info-item">
      <span class="label">账号状态：</span>
      <span class="value">{{ userInfo.accountStatus }}</span>
    </div>
    <div class="info-item">
      <span class="label">在职状态：</span>
      <span class="value">{{ userInfo.employmentStatus }}</span>
    </div>
    <div class="info-item">
      <span class="label">创建时间：</span>
      <span class="value">{{ userInfo.createTime }}</span>
    </div>
    <div class="info-item">
      <span class="label">最近一次登录时间：</span>
      <span class="value">{{ userInfo.lastLoginTime }}</span>
    </div>

    <!-- 修改密码弹窗 -->
    <el-dialog v-model="showPasswordModal" title="修改密码" width="500px" :close-on-click-modal="false">
      <div class="password-form">
        <el-form :model="passwordForm" :rules="passwordRules" ref="passwordFormRef" label-width="100px">
          <el-form-item label="原密码" prop="oldPassword" required>
            <el-input v-model="passwordForm.oldPassword" type="password" placeholder="请输入原密码" show-password />
          </el-form-item>
          <el-form-item label="新密码" prop="newPassword" required>
            <el-input v-model="passwordForm.newPassword" type="password" placeholder="请输入密码, 6~20个字符, 大写字母、小写字母、数字、特殊符号至少包含三种,必填" show-password />
          </el-form-item>
          <el-form-item label="确认新密码" prop="confirmPassword" required>
            <el-input v-model="passwordForm.confirmPassword" type="password" placeholder="再次输入新密码" show-password />
          </el-form-item>
        </el-form>
      </div>
      <template #footer>
        <div class="dialog-footer">
          <el-button @click="showPasswordModal = false">取消</el-button>
          <el-button type="primary" @click="handlePasswordSubmit">确定</el-button>
        </div>
      </template>
    </el-dialog>

    <!-- 默认密码提示弹窗 -->
    <el-dialog v-model="showDefaultPasswordModal" title="密码提示" width="400px" :close-on-click-modal="false">
      <div class="default-password-warning">
        <el-alert title="当前密码为默认密码,建议修改" type="warning" :closable="false" show-icon />
      </div>
      <template #footer>
        <div class="dialog-footer">
          <el-button @click="showDefaultPasswordModal = false">暂不修改</el-button>
          <el-button type="primary" @click="handleModifyPassword">修改</el-button>
        </div>
      </template>
    </el-dialog>
  </div>
</template>

<script setup>
import { ref, reactive, onMounted } from 'vue'
import { ElMessage } from 'element-plus'

const userInfo = reactive({
  account: 'YHBH001',
  name: '张三',
  dept: '销售一部',
  post: '销售',
  phone: '15200384759',
  accountStatus: '启用',
  employmentStatus: '在职',
  createTime: '2023/5/24 00:00:00',
  lastLoginTime: '2025/1/12 00:00:00'
})

const showPasswordModal = ref(false)
const showDefaultPasswordModal = ref(false)
const passwordFormRef = ref()

const passwordForm = reactive({
  oldPassword: '',
  newPassword: '',
  confirmPassword: ''
})

const validatePassword = (rule, value, callback) => {
  if (!value) {
    callback(new Error('请输入密码'))
  } else if (value.length < 6 || value.length > 20) {
    callback(new Error('密码长度应为6-20个字符'))
  } else {
    const hasUpper = /[A-Z]/.test(value)
    const hasLower = /[a-z]/.test(value)
    const hasNumber = /\d/.test(value)
    const hasSpecial = /[!@#$%^&*(),.?":{}|<>]/.test(value)
    const typeCount = [hasUpper, hasLower, hasNumber, hasSpecial].filter(Boolean).length
    if (typeCount < 3) {
      callback(new Error('密码必须包含大写字母、小写字母、数字、特殊符号中的至少三种'))
    } else {
      callback()
    }
  }
}

const passwordRules = {
  oldPassword: [{ required: true, message: '请输入原密码', trigger: 'blur' }],
  newPassword: [{ validator: validatePassword, trigger: 'blur' }],
  confirmPassword: [
    { required: true, message: '请确认新密码', trigger: 'blur' },
    {
      validator: (rule, value, callback) => {
        if (value !== passwordForm.newPassword) {
          callback(new Error('两次密码输入不一致'))
        } else {
          callback()
        }
      },
      trigger: 'blur'
    }
  ]
}

const handlePasswordSubmit = async () => {
  try {
    await passwordFormRef.value.validate()
    if (passwordForm.oldPassword !== 'zs8475') {
      ElMessage.error('原密码错误,请重新输入')
      return
    }
    if (passwordForm.newPassword !== passwordForm.confirmPassword) {
      ElMessage.error('两次密码输入不一致,请重新输入')
      return
    }
    ElMessage.success('修改成功,请重新登录')
    showPasswordModal.value = false
    Object.assign(passwordForm, { oldPassword: '', newPassword: '', confirmPassword: '' })
    setTimeout(() => {
      console.log('跳转到登录页')
    }, 3000)
  } catch (error) {
    console.error('表单验证失败:', error)
  }
}

const handleModifyPassword = () => {
  showDefaultPasswordModal.value = false
  showPasswordModal.value = true
}

onMounted(() => {
  // 可在此检查默认密码并决定是否展示提示弹窗
})
</script>

<style lang="scss" scoped>
.info-item {
  display: flex;
  align-items: center;
  margin-bottom: 15px;
  font-size: 14px;
}

.info-item .label {
  width: 150px;
  color: #606266;
}

.info-item .value {
  flex: 1;
  color: #303133;
}
.info-item .account_value {
  flex: 0;
  margin-right: 10px;
}

.password-form {
  padding: 20px 0;
}

.dialog-footer {
  text-align: right;
}

.default-password-warning {
  padding: 20px 0;
}
</style>
